package com.deep.service.impl;


import com.deep.dto.DeepSeekRequest;
import com.deep.enums.RoleEnum;
import com.deep.service.DeepSeekService;
import com.deep.vo.DeepSeekResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

import static com.deep.config.DeepSeekProperties.*;
import static com.deep.constant.RequestConstant.*;

@Service
public class DeepSeekImpl implements DeepSeekService {

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public String requestDeepSeek(String question) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set(CONTENT_TYPE, APPLICATION_JSON);
        httpHeaders.set(ACCEPT, APPLICATION_JSON);
        httpHeaders.set(AUTHORIZATION, API_KEY);

        DeepSeekRequest request = new DeepSeekRequest();
        request.setModel(MODEL);
        request.setStream(false);
        request.setMessages(List.of(new DeepSeekRequest.Message(RoleEnum.USER.getRole(), question)));

        HttpEntity<DeepSeekRequest> httpEntity = new HttpEntity<>(request, httpHeaders);

        ResponseEntity<DeepSeekResponse> response = restTemplate.exchange(REQUEST_URL, HttpMethod.POST, httpEntity, DeepSeekResponse.class);
        if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
            return response.getBody().getChoices().getFirst().getMessage().getContent();
        } else {
            return "未获取到内容信息，请重试一次！";
        }
    }
}
